package com.force.langchain4j.tika;

import org.apache.tika.detect.EncodingDetector;
import org.apache.tika.parser.html.HtmlEncodingDetector;
import org.apache.tika.parser.txt.UniversalEncodingDetector;
import org.apache.tika.parser.txt.Icu4jEncodingDetector;


import java.util.ArrayList;
import java.util.List;

public class TikaEncodingConfig {

    public static List<EncodingDetector> createEncodingDetectors() {
        List<EncodingDetector> encodingDetectors = new ArrayList<>();

        // HtmlEncodingDetector
        HtmlEncodingDetector htmlEncodingDetector = new HtmlEncodingDetector();
        htmlEncodingDetector.setMarkLimit(64000);
        encodingDetectors.add(htmlEncodingDetector);

        // UniversalEncodingDetector
        UniversalEncodingDetector universalEncodingDetector = new UniversalEncodingDetector();
        universalEncodingDetector.setMarkLimit(64000);
        encodingDetectors.add(universalEncodingDetector);

        // Icu4jEncodingDetector
        Icu4jEncodingDetector icu4jEncodingDetector = new Icu4jEncodingDetector();
        icu4jEncodingDetector.setMarkLimit(64000);
        encodingDetectors.add(icu4jEncodingDetector);

        return encodingDetectors;
    }

    public static void main(String[] args) {
        List<EncodingDetector> detectors = createEncodingDetectors();

        for (EncodingDetector detector : detectors) {
            System.out.println("Detector: " + detector.getClass().getName()
                    + ", Mark Limit: " + getMarkLimit(detector));
        }
    }

    // 获取 markLimit 值的辅助方法（假设这些类都有 getMarkLimit 方法）
    private static int getMarkLimit(EncodingDetector detector) {
        if (detector instanceof HtmlEncodingDetector) {
            return ((HtmlEncodingDetector) detector).getMarkLimit();
        } else if (detector instanceof UniversalEncodingDetector) {
            return ((UniversalEncodingDetector) detector).getMarkLimit();
        } else if (detector instanceof Icu4jEncodingDetector) {
            return ((Icu4jEncodingDetector) detector).getMarkLimit();
        }
        return -1;
    }
}
